﻿#if !WINDOWS_PHONE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework;

namespace JGLib.Settings
{
    /// <summary>The static class that houses utilities for retrieving storage space.</summary>
    //[System.Diagnostics.DebuggerNonUserCode]
    public static class Storage
    {
        /// <summary>The delegate type for retrieval of a storage device.</summary>
        /// <param name="device">The storage device that was retrieved.</param>
        public delegate void GetStorageDevice(StorageDevice device);

        /// <summary>Starts the loading of a storage device, which will be returned through the callback.</summary>
        /// <param name="callback">The function that will retrieve the storage device.</param>
        public static void StartLoadStorageDevice(GetStorageDevice callback)
        {
            StorageDevice.BeginShowSelector(PlayerIndex.One,
            (IAsyncResult result) =>
            {
                StorageDevice device = StorageDevice.EndShowSelector(result);
                if (device == null || !device.IsConnected)
                    throw new Exception("Error retrieving storage device. (JGLib.Settings.Storage.StartLoadStorageDevice()).");
                callback(device);
            },
            null);
        }

        internal static StorageContainer GetStorageContainer(StorageDevice storage, string titleName)
        {
            IAsyncResult result = storage.BeginOpenContainer(titleName, null, null);
            result.AsyncWaitHandle.WaitOne();
            StorageContainer container = storage.EndOpenContainer(result);
            result.AsyncWaitHandle.Close();
            return container;
        }
    }
}
#endif